package com.sxh.permission.map;

import com.sxh.permission.model.UserRoles;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.Mapping;

import java.util.List;

@Mapper
public interface PermissionMap {
    @Insert("insert into user_roles(user_id, role_id) values(#{userId},#{roleId})")
    int bindRole(UserRoles userRole);
    //根据权限id查询用户id
    @Select("select user_id from user_roles where role_id = " +
            "(select role_id from roles where role_code like #{rodeCode})")
    List<Long> queryUserIdByRoleCode(String rodeCode);
    //查询所有用户id
    @Select("select user_id from user_roles")
    List<Long> queryAllUserId();
    //根据用户id查询角色id
//    @Select("select role_id from user_roles where user_id = #{userId}")
//    Integer getUserRoleId(Long userId);
    //根据用户id查询角色码
    @Select("select role_code from roles where role_id = (select role_id from user_roles where user_id = #{userId})")
    String getUserRoleCode(Long userId);

    //修改用户权限
    @Update("update user_roles set role_id = (select role_id from roles where role_code like #{roleCode}) where user_id = #{userId}")
    void updateGrade(Long userId,String roleCode);
}
